<?php
    include($_SERVER['DOCUMENT_ROOT']."/ObstacleWebApp/view/headeradmin.php");
?>

<section>
    <script>
        $(function() {
            $("#search").autocomplete({
            source: "zoekgebruiker.php",
            minLength: 2
            });
        });
    </script>
<hgroup>
<h1>Gebruikers beheren</h1>
</hgroup>

    <br>
<form  name="registerform" method="get" action="<?php echo $_SERVER['PHP_SELF']; ?>">
    <?php
        if (isset($_GET['search'])) // If a search has been given, $_GET['search'] is known and the text-field will be filled with the search-string
        { ?>
            <input placeholder="Achternaam" name="search" id="search" value="<?php echo $_GET['search']; ?>" />
        <?php }
        else // If no seach has been given, $_GET['search'] will be unknown and the text-field will be left empty
        { ?>
            <input placeholder="Achternaam" name="search" id="search" />
    <?php    }
    ?>
    
    
    <input type="submit" name="Submit" value="Zoeken">
</form>
    <br>
    
    Klik <a href="gebruikertoevoegen.php">hier</a> om een nieuwe gebruiker toe te voegen.
    <br><br>
    Klik <a href="functies.php">hier</a> om de verschillende functies te beheren.
    <br><br>
<?php
if (isset($_GET['start'])) // If we are no longer on the first page, $_GET['start'] will be known and it's value will be saved in $start
{
    $start=$_GET['start'];
}
else // If we are still on the first page, $_GET['start'] will be unknown and a value of 0 will be saved in $start
{
    $start=0;
}

if(strlen($start) > 0 and !is_numeric($start)){ // A check on the value of $start
	exit;
}
$eu = ($start - 0);
$limit = 20; // Number of records shown on each page
$thi = $eu + $limit;
$back = $eu - $limit;
$next = $eu + $limit;

if (isset($_GET['search'])) // The SQL-commands when a search has been given, even when the search-string is empty
{
    $page_name="gebruikers.php?search=".$_GET['search']."&";
    if ($_GET['search'] == "")
    {
        $nume = GetAllUserCount();
    }
    else
    {
        $nume = GetAllUserCountWithSearch($_GET['search']);
    }
}
else // The SQL-command when no search has been given
{
    $page_name="gebruikers.php?";
    $nume = GetAllUserCount();
}
?>

<table id="rounded-corner">
    <thead>
    	<tr>
        	<th scope="col" class="rounded-company">Gebruikersnaam</th>
            <th scope="col" class="rounded-q2">Voornaam</th>
            <th scope="col" class="rounded-q3">Achternaam</th>
            <th scope="col" class="rounded-q3">Email</th>
            <th scope="col" class="rounded-q3">Functie</th>
            <th scope="col" class="rounded-q3">Geboortedatum</th>
            <th scope="col" class="rounded-q3">Admin</th>
            <th scope="col" class="rounded-q3">Status</th>
            <th scope="col" class="rounded-q3">Bewerken</th>
            <th scope="col" class="rounded-q4">Deactiveren</th>
        </tr>
    </thead>
        <tfoot>
    	<tr>
        	<td colspan="9" class="rounded-foot-left">
        	<?php
	        	if ( $limit < $nume) { // Creating the links for the different pages
	        		if($back >=0) {
						echo "<a href=".$page_name."start=".$back.">Vorige</a>";
					}
	        		$i=0;
					$l=1;
					for($i=0;$i < $nume;$i=$i+$limit){
						if($i <> $eu)
						{
							echo " <a href=".$page_name."start=".$i.">$l</a> ";
						}
						else 
						{ 
							echo "<span style=\"font-weight:bold;\">".$l."</span>";
						}
						$l=$l+1;
					}
	        		if($thi < $nume) 
	        		{
						echo "<a href=".$page_name."start=".$next.">Volgende</a>";
					}
				}
        	?>
        	</td>
        	<td class="rounded-foot-right">&nbsp;</td>
        </tr>
    </tfoot>
    <tbody>
<?php
// Getting all the records for a specific spage (e.g. first page, second page, ...)
if (isset($_GET['search']))
{
    if ($_GET['search'] == "")
    {
        $getAllUsers = GetAllUserLimit($eu, $limit);
    }
    else 
    {
        $getAllUsers = GetAllUserLimitWithSearch($eu, $limit, $_GET['search']);
    }    
}
else
{
    $getAllUsers = GetAllUserLimit($eu, $limit);
}


while($userRow = mysql_fetch_array($getAllUsers))
{

echo "<tr><td>".$userRow['UserName']."</td>";
echo "<td>" . $userRow['FirstName']."</td>";
echo "<td>" . $userRow['LastName']."</td>";
echo "<td>" . $userRow['Email']."</td>";
echo "<td>" . $userRow['Function']."</td>";
$dayofbirth = date("d-m-Y",strtotime($userRow['DayOfBirth']));
echo "<td>" . $dayofbirth."</td>";
if ($userRow['Admin'] == "T")
{
    $admin = "Ja";
}
else
{
    $admin = "Nee";
}
echo "<td>" . $admin."</td>";
if ($userRow['Activated'] == "T")
{
    $activated = "Geactiveerd";
}
else
{
    $activated = "Gedeactiveerd";
}
echo "<td>" . $activated."</td>";

if (isset($_GET['start'])) {
    if (isset($_GET['search'])) {
        echo "<td><a href=\"gebruikeraanpassen.php?id=" . $userRow['Id'] . "&search=" . $_GET['search'] . "&start=" . $_GET['start'] . "\" >Bewerken</a></td>";
        if ($userRow['Activated'] == "T")
        {
            echo "<td><a href=\"gebruikerdeactiveren.php?id=".$userRow['Id']."&search=" . $_GET['search'] . "&start=" . $_GET['start'] . "\" >Deactiveren</a></td></tr>";
        }
        else
        {
            echo "<td><a href=\"gebruikeractiveren.php?id=".$userRow['Id']."&search=" . $_GET['search'] . "&start=" . $_GET['start'] . "\" >Activeren</a></td></tr>";
        }
    } else {
        echo "<td><a href=\"gebruikeraanpassen.php?id=" . $userRow['Id'] . "&start=" . $_GET['start'] . "\" >Bewerken</a></td>";
        if ($userRow['Activated'] == "T")
        {
            echo "<td><a href=\"gebruikerdeactiveren.php?id=".$userRow['Id']."&start=" . $_GET['start'] . "\" >Deactiveren</a></td></tr>";
        }
        else
        {
            echo "<td><a href=\"gebruikeractiveren.php?id=".$userRow['Id']."&start=" . $_GET['start'] . "\" >Activeren</a></td></tr>";
        }
    }
} else {
    if (isset($_GET['search'])) {
        echo "<td><a href=\"gebruikeraanpassen.php?id=" . $userRow['Id'] . "&search=" . $_GET['search'] . "\" >Bewerken</a></td>";
        if ($userRow['Activated'] == "T")
        {
            echo "<td><a href=\"gebruikerdeactiveren.php?id=".$userRow['Id']."&search=" . $_GET['search'] . "\" >Deactiveren</a></td></tr>";
        }
        else
        {
            echo "<td><a href=\"gebruikeractiveren.php?id=".$userRow['Id']."&search=" . $_GET['search'] . "\" >Activeren</a></td></tr>";
        }
    } else {
        echo "<td><a href=\"gebruikeraanpassen.php?id=" . $userRow['Id'] . "\" >Bewerken</a></td>";
        if ($userRow['Activated'] == "T")
        {
            echo "<td><a href=\"gebruikerdeactiveren.php?id=".$userRow['Id']."\" >Deactiveren</a></td></tr>";
        }
        else
        {
            echo "<td><a href=\"gebruikeractiveren.php?id=".$userRow['Id']."\" >Activeren</a></td></tr>";
        }
    }
}

}

?>
</tbody>
</table>


</section>

<?php
    include($_SERVER['DOCUMENT_ROOT']."/ObstacleWebApp/view/footer.php");
?>